package com.example.recyclerview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 * @author by liliang on 2020/5/26 4:39 PM
 */
class HomeAdapter(strList: MutableList<String>, heightList: MutableList<Int>, context: Context) :
    RecyclerView.Adapter<HomeAdapter.ViewHolder>() {

    private val mList = strList
    private val mHeightList = heightList
    private val mContext = context
    private lateinit var mOnItemClickListener: OnItemClickListener

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textView: TextView = view.findViewById(R.id.text_item)

    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.mOnItemClickListener = onItemClickListener
    }

    fun removeData(position: Int) {
        mList.removeAt(position)
        notifyItemRemoved(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = mList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.layoutParams.apply { height = mHeightList[position]}

        holder.textView.text = mList[position]
        holder.textView.setOnClickListener {
            val pos = holder.layoutPosition
            mOnItemClickListener.onItemClick(holder.textView, pos)
        }
        holder.textView.setOnLongClickListener {
            val pos = holder.layoutPosition
            mOnItemClickListener.onItemLongClick(holder.textView, pos)
            return@setOnLongClickListener false
        }
    }
}